package com.shipwe.loans.ui.activity

import android.annotation.SuppressLint
import android.graphics.Color
import android.os.CountDownTimer
import android.view.View
import android.view.animation.Animation
import android.view.animation.RotateAnimation
import com.shipwe.loans.R
import com.shipwe.loans.base.app.ShipweStackManager
import com.shipwe.loans.databinding.ActivityAuthStepsCompleteWaitBinding
import com.shipwe.loans.pro.ShipweActivity
import com.shipwe.loans.utils.event.AppEventPointType
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch


/**
 * Shipwe
 * @Author wwdeng
 * @CreateTime: 2024/11/22 18:45
 * @Annotation：进件信息完成等待
 */
class ActivityAuthStepsCompleteWait: ShipweActivity(), CoroutineScope by MainScope() {
    lateinit var binding: ActivityAuthStepsCompleteWaitBinding
    private var countdownTimer: MyCountDownTimer? = null
    //等待时长
    private var countdown = 9

    override fun getShipweRootView(): View {
        binding = ActivityAuthStepsCompleteWaitBinding.inflate(layoutInflater)
        return binding.root
    }

    override fun initShipweView() {
        rootViewBinding.rootLayout.setBackgroundResource(R.drawable.page_login_bg)
        setShipweTitleBarVisibility(View.VISIBLE)
        setShipweBackBtnVisibility(View.INVISIBLE)
        setShipweRightBtnRes(R.drawable.customer_black)
        setShipweTextTitleVisibility(View.GONE)
    }

    override fun initShipweListener() {
        //联系客服
        rootViewBinding.rootCustomer.setOnClickListener {
            showCustomerDialog("Step Complete Wait Page")
            appEventPointRequestAction(AppEventPointType.CLICK.SHIPWE_CLICK_CUSTOMER,"Step Complete Wait Page")
        }
    }

    override fun initShipweData() {
        if (countdownTimer == null){
            countdownTimer = MyCountDownTimer(this,9000,1000)
        }
        countdownTimer?.start()
    }

    class MyCountDownTimer(private val act: ActivityAuthStepsCompleteWait,millisInFuture: Long, countDownInterval: Long): CountDownTimer(millisInFuture, countDownInterval) {
        override fun onTick(millisUntilFinished: Long) {
            // 这里每隔一段时间（定时器间隔）会被调用
            // 可以在这里更新UI，显示剩余时间
            act.countdown = (millisUntilFinished / 1000).toInt()
            act.showAuthStepsCompleteTipsText()
        }
        override fun onFinish() {
            // 倒计时完成时被调用
            act.showAuthStepsCompleteSuccess()
        }
    }

    /**
     * 设置文本
     */
    @SuppressLint("SetTextI18n")
    private fun showAuthStepsCompleteTipsText(){
        binding.txtCountdown.text = "${countdown}s"
        showAuthStepsCompleteStatusView()
    }

    private fun showAuthStepsCompleteSuccess(){
        showShipweLoading()
        launch(Dispatchers.Main){
            delay(500)
            dismissShipweLoading()
            //返回首页
            ShipweStackManager.finishAllActivity(ActivityMain::class.java)
        }
    }

    /**
     *旋转动画
     */
    private fun getAuthStepsCompleteAnimation(): Animation{
        // 创建一个旋转动画对象，从当前的角度旋转到指定的角度，持续时间为1000毫秒
        val rotateAnimation = RotateAnimation(0F, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
        rotateAnimation.setDuration(1000L) // 设置动画持续时间
//        rotateAnimation.fillAfter = false // 动画执行完后保持结束状态
        rotateAnimation.isFillEnabled = true;
        rotateAnimation.fillBefore = false;
        rotateAnimation.setRepeatCount(Animation.INFINITE); // 设置动画重复次数
        return rotateAnimation
    }

    /**
     * 显示状态
     */
    private fun showAuthStepsCompleteStatusView(){
        if (countdown in 7..9){
            binding.imgStep1.setImageResource(R.drawable.wait_progress_icon)
            binding.txtStep1.setTextColor(Color.parseColor("#FF000000"))
            binding.imgStep1.startAnimation(getAuthStepsCompleteAnimation())

            binding.imgStep2.setImageResource(R.drawable.wait_countdown_icon)
            binding.txtStep2.setTextColor(Color.parseColor("#FF88888B"))

            binding.imgStep3.setImageResource(R.drawable.wait_countdown_icon)
            binding.txtStep3.setTextColor(Color.parseColor("#FF88888B"))
            return
        }

        if (countdown in 4..6){
            binding.imgStep1.setImageResource(R.drawable.select_theme)
            binding.txtStep1.setTextColor(Color.parseColor("#FF88888B"))
            binding.imgStep1.clearAnimation()

            binding.imgStep2.setImageResource(R.drawable.wait_progress_icon)
            binding.txtStep2.setTextColor(Color.parseColor("#FF000000"))
            binding.imgStep2.startAnimation(getAuthStepsCompleteAnimation())

            binding.imgStep3.setImageResource(R.drawable.wait_countdown_icon)
            binding.txtStep3.setTextColor(Color.parseColor("#FF88888B"))
            return
        }
        binding.imgStep1.setImageResource(R.drawable.select_theme)
        binding.txtStep1.setTextColor(Color.parseColor("#FF88888B"))
        binding.imgStep1.clearAnimation()

        binding.imgStep2.setImageResource(R.drawable.select_theme)
        binding.txtStep2.setTextColor(Color.parseColor("#FF88888B"))
        binding.imgStep2.clearAnimation()

        if (countdown >= 2){
            binding.imgStep3.setImageResource(R.drawable.wait_progress_icon)
            binding.txtStep3.setTextColor(Color.parseColor("#FF000000"))
            binding.imgStep3.startAnimation(getAuthStepsCompleteAnimation())
            return
        }
        binding.imgStep3.setImageResource(R.drawable.select_theme)
        binding.txtStep3.setTextColor(Color.parseColor("#FF88888B"))
        binding.imgStep3.clearAnimation()
    }

    override fun onStart() {
        super.onStart()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_STEP_CHECK_WAIT_START)
    }

    override fun onDestroy() {
        super.onDestroy()
        appEventPointRequestAction(AppEventPointType.PAGE.SHIPWE_PAGE_STEP_CHECK_WAIT_END)
        if (countdownTimer != null){
            countdownTimer = null
        }
    }
}